﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Business.Entity;
namespace Chenxin.Lib.Business.BLL
{
    /// <summary>
    /// 业务逻辑类ServiceChargeBaseInfo 的摘要说明。
    /// </summary>
    public class ServiceChargeBaseInfo
    {

        public ServiceChargeBaseInfo()
        { }
        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo model)
        {
            Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo model)
        {
            Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.Update(model);
        }

        /// <summary>
        /// 获得收据编号
        /// </summary>
        /// <param name="CompanyId">公司ID</param>
        /// <returns></returns>
        public string GenerateCode(Guid CompanyId)
        {
            return Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.GenerateCode(CompanyId);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid ReceiptId)
        {

            Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.Delete(ReceiptId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo GetModel(Guid ReceiptId)
        {

            return Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.GetModel(ReceiptId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.GetList(strWhere);
        }

        public DataSet GetListByConditions(Dictionary<string, string> dicParams)
        {
            System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
            int PageSize = 10;
            int PageIndex = 1;

            string TableName = "t_ServiceCharge_BaseInfo";
            string OrderField = "AddTime DESC";
            string FieldList = "ReceiptId,Status,ReceiptCode,ReceiptDepart,ReceiptEmployee,GetedTime,ContractCode,BusinessType,HouseAddress,CustomerName,Mobile,Phone,CustomerAddress,ZipCode,ReceiptMoneyName,GetedMoney,AddTime,AddEmployee,ReceiptCityId,ReceiptCompanyId,ReceiptDepartId,ReceiptDepartId,ReceiptEmployeeId,ReceiptCity,ReceiptCompany,ReceiptDepart,ReceiptEmployee,ReceiptMemo";
            string TableFieldList = "(Case When Status=1 Then '正常' Else '作废' End) As Status," +
                                    "ReceiptCode," +
                                    "ReceiptDepart," +
                                    "ReceiptEmployee," +
                                    "REPLACE(CONVERT(Varchar(10),GetedTime,111),'/','-') As GetedTime," +
                                    "ContractCode," +
                                    "(Case When BusinessType=10 Then '出售' Else '出租' End) As BusinessType," +
                                    "HouseAddress," +
                                    "CustomerName," +
                                    "Mobile," +
                                    "Phone," +
                                    "CustomerAddress," +
                                    "ZipCode," +
                                    "ReceiptMoneyName," +
                                    "GetedMoney," +
                                    "AddTime," +
                                    "AddEmployee," +
                                    "ReceiptId," +
                                    "ReceiptCityId," +
                                    "ReceiptCompanyId," +
                                    "ReceiptDepartId," +
                                    "ReceiptEmployeeId," +
                                    "ReceiptCity," +
                                    "ReceiptCompany," +
                                    "ReceiptMemo";


            strWhere.Append(" 1=1 ");
            if (dicParams.ContainsKey("PageSize"))
            {
                PageSize = Convert.ToInt32(dicParams["PageSize"]);
            }
            if (dicParams.ContainsKey("PageIndex"))
            {
                PageIndex = Convert.ToInt32(dicParams["PageIndex"]) + 1;
            }
            if (dicParams.ContainsKey("Sort"))
            {
                OrderField = dicParams["Sort"];
            }
            if (dicParams.ContainsKey("StartTime"))
            {
                strWhere.AppendFormat(" And GetedTime>='{0}'", dicParams["StartTime"]);
            }
            if (dicParams.ContainsKey("EndTime"))
            {
                strWhere.AppendFormat(" And GetedTime<'{0}'", DateTime.Parse(dicParams["EndTime"]).AddDays(1).ToString("yyyy-MM-dd"));
            }
            if (dicParams.ContainsKey("Status"))
            {
                strWhere.AppendFormat(" And Status = {0}", dicParams["Status"]);
            }
            else
            {
                strWhere.AppendFormat(" And Status > {0}", "-1");
            }
            if (dicParams.ContainsKey("Keyword"))
            {
                strWhere.AppendFormat(" And (ReceiptCode Like '%{0}%' Or ContractCode Like '%{0}%' Or HouseAddress Like '%{0}%')", dicParams["Keyword"]);
            }
            if (dicParams.ContainsKey("CityId"))
            {
                strWhere.AppendFormat(" And ReceiptCityId = '{0}'", dicParams["CityId"]);
            }
            if (dicParams.ContainsKey("CompanyId"))
            {
                strWhere.AppendFormat(" And ReceiptCompanyId = '{0}'", dicParams["CompanyId"]);
            }
            if (dicParams.ContainsKey("AreaId"))
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo AreaInfo = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, dicParams["AreaId"]);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                AreaInfo = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    AreaInfo = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(new Guid(dicParams["AreaId"]));
                    if (AreaInfo != null) cs.Set(strCachkey, AreaInfo, DateTime.Now.AddDays(1));
                }
                strWhere.AppendFormat(" And Exists(Select 1 From v_Hr_DepartmentInfo Where DepartId = ReceiptDepartId And DepartSrc Like '{0}%')", AreaInfo.DepartSrc);
            }
            if (dicParams.ContainsKey("DepartId"))
            {
                strWhere.AppendFormat(" And ReceiptDepartId = '{0}'", dicParams["DepartId"]);
            }
            if (dicParams.ContainsKey("UserId"))
            {
                strWhere.AppendFormat(" And ReceiptEmployeeId = '{0}'", dicParams["UserId"]);
            }
            if (dicParams.ContainsKey("CurDepartId"))
            {
                strWhere.AppendFormat(" And ReceiptDepartId in (select DepartId from t_Business_EmpSetting where UserId='{0}' and SearchCharge=1)", dicParams["CurDepartId"]);
            }
            if (dicParams.ContainsKey("CurUserId"))
            {
                strWhere.AppendFormat(" And ReceiptEmployeeId = '{0}'", dicParams["CurUserId"]);
            }

            return Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.GetPartList(PageSize, PageIndex, OrderField, TableName, strWhere.ToString(), FieldList, TableFieldList, true);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Business.DAL.ServiceChargeBaseInfo.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo> modelList = new List<Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo();
                    if (dt.Rows[n]["ReceiptId"].ToString() != "")
                    {
                        model.ReceiptId = new Guid(dt.Rows[n]["ReceiptId"].ToString());
                    }
                    model.ReceiptCode = dt.Rows[n]["ReceiptCode"].ToString();
                    if (dt.Rows[n]["ReceiptCityId"].ToString() != "")
                    {
                        model.ReceiptCityId = new Guid(dt.Rows[n]["ReceiptCityId"].ToString());
                    }
                    model.ReceiptCity = dt.Rows[n]["ReceiptCity"].ToString();
                    if (dt.Rows[n]["ReceiptCompanyId"].ToString() != "")
                    {
                        model.ReceiptCompanyId = new Guid(dt.Rows[n]["ReceiptCompanyId"].ToString());
                    }
                    model.ReceiptCompany = dt.Rows[n]["ReceiptCompany"].ToString();
                    if (dt.Rows[n]["ReceiptDepartId"].ToString() != "")
                    {
                        model.ReceiptDepartId = new Guid(dt.Rows[n]["ReceiptDepartId"].ToString());
                    }
                    model.ReceiptDepart = dt.Rows[n]["ReceiptDepart"].ToString();
                    if (dt.Rows[n]["ReceiptEmployeeId"].ToString() != "")
                    {
                        model.ReceiptEmployeeId = new Guid(dt.Rows[n]["ReceiptEmployeeId"].ToString());
                    }
                    model.ReceiptEmployee = dt.Rows[n]["ReceiptEmployee"].ToString();
                    if (dt.Rows[n]["BusinessType"].ToString() != "")
                    {
                        model.BusinessType = int.Parse(dt.Rows[n]["BusinessType"].ToString());
                    }
                    model.ReceiptMoneyName = dt.Rows[n]["ReceiptMoneyName"].ToString();
                    if (dt.Rows[n]["ContractId"].ToString() != "")
                    {
                        model.ContractId = new Guid(dt.Rows[n]["ContractId"].ToString());
                    }
                    model.ContractCode = dt.Rows[n]["ContractCode"].ToString();
                    model.HouseAddress = dt.Rows[n]["HouseAddress"].ToString();
                    model.CustomerName = dt.Rows[n]["CustomerName"].ToString();
                    model.Mobile = dt.Rows[n]["Mobile"].ToString();
                    model.Phone = dt.Rows[n]["Phone"].ToString();
                    model.CustomerAddress = dt.Rows[n]["CustomerAddress"].ToString();
                    model.ZipCode = dt.Rows[n]["ZipCode"].ToString();
                    if (dt.Rows[n]["GetedTime"].ToString() != "")
                    {
                        model.GetedTime = DateTime.Parse(dt.Rows[n]["GetedTime"].ToString());
                    }
                    if (dt.Rows[n]["GetedMoney"].ToString() != "")
                    {
                        model.GetedMoney = decimal.Parse(dt.Rows[n]["GetedMoney"].ToString());
                    }
                    model.ReceiptMemo = dt.Rows[n]["ReceiptMemo"].ToString();
                    if (dt.Rows[n]["AddEmployeeId"].ToString() != "")
                    {
                        model.AddEmployeeId = new Guid(dt.Rows[n]["AddEmployeeId"].ToString());
                    }
                    model.AddEmployee = dt.Rows[n]["AddEmployee"].ToString();
                    if (dt.Rows[n]["AddTime"].ToString() != "")
                    {
                        model.AddTime = DateTime.Parse(dt.Rows[n]["AddTime"].ToString());
                    }
                    if (dt.Rows[n]["Status"].ToString() != "")
                    {
                        model.Status = int.Parse(dt.Rows[n]["Status"].ToString());
                    }
                    if (dt.Rows[n]["IsGeted1"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsGeted1"].ToString() == "1") || (dt.Rows[n]["IsGeted1"].ToString().ToLower() == "true"))
                        {
                            model.IsGeted1 = true;
                        }
                        else
                        {
                            model.IsGeted1 = false;
                        }
                    }
                    if (dt.Rows[n]["IsGeted2"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsGeted2"].ToString() == "1") || (dt.Rows[n]["IsGeted2"].ToString().ToLower() == "true"))
                        {
                            model.IsGeted2 = true;
                        }
                        else
                        {
                            model.IsGeted2 = false;
                        }
                    }
                    model.FinMemo = dt.Rows[n]["FinMemo"].ToString();
                    if (dt.Rows[n]["IsConfirmed"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsConfirmed"].ToString() == "1") || (dt.Rows[n]["IsConfirmed"].ToString().ToLower() == "true"))
                        {
                            model.IsConfirmed = true;
                        }
                        else
                        {
                            model.IsConfirmed = false;
                        }
                    }
                    if (dt.Rows[n]["LastConfirmeTime"].ToString() != "")
                    {
                        model.LastConfirmeTime = DateTime.Parse(dt.Rows[n]["LastConfirmeTime"].ToString());
                    }
                    if (dt.Rows[n]["LastConfirmeEmpId"].ToString() != "")
                    {
                        model.LastConfirmeEmpId = new Guid(dt.Rows[n]["LastConfirmeEmpId"].ToString());
                    }
                    model.LastConfirmeEmployee = dt.Rows[n]["LastConfirmeEmployee"].ToString();
                    if (dt.Rows[n]["ConfireGetedTime"].ToString() != "")
                    {
                        model.ConfireGetedTime = DateTime.Parse(dt.Rows[n]["ConfireGetedTime"].ToString());
                    }
                    if (dt.Rows[n]["FinishFlag"].ToString() != "")
                    {
                        model.FinishFlag = int.Parse(dt.Rows[n]["FinishFlag"].ToString());
                    }
                    if (dt.Rows[n]["LastPrintTime"].ToString() != "")
                    {
                        model.LastPrintTime = DateTime.Parse(dt.Rows[n]["LastPrintTime"].ToString());
                    }
                    if (dt.Rows[n]["LastPrintEmpId"].ToString() != "")
                    {
                        model.LastPrintEmpId = new Guid(dt.Rows[n]["LastPrintEmpId"].ToString());
                    }
                    model.LastPrintEmp = dt.Rows[n]["LastPrintEmp"].ToString();
                    if (dt.Rows[n]["TotalPrintCount"].ToString() != "")
                    {
                        model.TotalPrintCount = int.Parse(dt.Rows[n]["TotalPrintCount"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }


        #endregion  成员方法
    }
}

